<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\admin\v1\agent;


use app\controller\admin\AuthController;
use app\model\agent\AgentCommission as AgentCommissionModel;
use FormBuilder\Exception\FormBuilderException;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Response;

/**
 * Class AgentLevel
 * @package app\controller\admin\v1\agent
 */
class AgentCommission extends AuthController
{

    
    /**
     * 显示资源列表
     *
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['limit', 1],
            ['page', 10]
        ]);
        
        $list = AgentCommissionModel::paginate($where['limit']);
        $data = ['data'=>$list];
        // var_dump($list->items());die;
        return $this->success($data);
    }
    
    

    /**
     * 显示创建资源表单页.
     *
     * @return Response
     * @throws FormBuilderException
     */
    public function create()
    {
        return $this->success($this->services->createForm());
    }

    /**
     * 保存新建的资源
     *
     * @return Response
     */
    public function save()
    {
        $data = $this->request->postMore([
            ['title', ''],
            ['min_money', 0],
            ['max_money', 0],
            ['commission_rate', ''],
            ]);
      
        if (!$data['title']) return $this->fail('请输入标题');
        if (!$data['min_money']) return $this->fail('请输入最小业绩金额');
        if (!$data['max_money']) return $this->fail('请输入最大业绩金额');
        if (!$data['commission_rate']) return $this->fail('请选择分佣比例');
       
        $commission_model = new AgentCommissionModel();
        $commission_model -> save($data);
       
        return $this->success('添加等级成功!');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return Response
     * @throws FormBuilderException
     */
    public function edit($id)
    {
        return $this->success($this->services->editForm((int)$id));
    }

    /**
     * 保存更新的资源
     *
     * @param int $id
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function update()
    {
       $data = $this->request->postMore([
            ['id',''],
            ['title', ''],
            ['min_money', 0],
            ['max_money', 0],
            ['commission_rate', ''],
            ]);
      
        if (!$data['title']) return $this->fail('请输入标题');
        if (!$data['min_money']) return $this->fail('请输入最小业绩金额');
        if (!$data['max_money']) return $this->fail('请输入最大业绩金额');
        if (!$data['commission_rate']) return $this->fail('请选择分佣比例');

       
        $info = AgentCommissionModel::where('id',$data['id'])->find();
        if(!$info){
            return $this->fail('数据不存在');
        }
      
        $info->save($data);
        return $this->success('修改成功!');
    }

    /**
     * 删除指定资源
     * @param $id
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function delete($id)
    {
        if (!$id) return $this->fail('参数错误，请重新打开');
        $info = AgentCommissionModel::where('id',$id)->find();
        if(!$info){
            return $this->fail('数据不存在');
        }
        AgentCommissionModel::where('id',$id)->delete();
        return $this->success('删除成功!');
    }

    /**
     * 修改状态
     * @param int $id
     * @param string $status
     * @return mixed
     */
    public function set_status($id = 0, $status = '')
    {
        if ($status == '' || $id == 0) return $this->fail('参数错误');
        $this->services->update($id, ['status' => $status]);
        return $this->success($status == 0 ? '隐藏成功' : '显示成功');
    }
}
